﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace DonationTracker
{
    public partial class Donors : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Administrator"))
                {
                    GridView1.AutoGenerateEditButton = true;
                    GridView1.AutoGenerateDeleteButton = true;
                }
                else if (User.IsInRole("Volunteer"))
                {
                    GridView1.AutoGenerateEditButton = true;
                }

                if (!IsPostBack) GridView1.ShowFooter = false;
            }
        }

        protected void btnAddDonor_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = true;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert" && Page.IsValid)
            {
                //Locate the controls in the Gridview's footer and access them.
                TextBox txtNewFirstName =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewFirstName");
                TextBox txtNewLastName =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewLastName");
                TextBox txtNewContactNumber =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewContactNumber");
                TextBox txtNewEmail = 
                    (TextBox)GridView1.FooterRow.FindControl("txtNewEmail");
                TextBox txtNewOrganization =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewOrganization");
                DropDownList ddlDonorType =
                    (DropDownList)GridView1.FooterRow.FindControl("ddlDonorType");

                //Trim any extra spaces from the beginning/end of text.
                string first, last, num, email, org, type;
                first = txtNewFirstName.Text.Trim();
                last = txtNewLastName.Text.Trim();
                num = txtNewContactNumber.Text.Trim();
                email = txtNewEmail.Text.Trim();
                org = txtNewOrganization.Text.Trim();
                type = ddlDonorType.SelectedValue;
                //If the textboxes are empty, set them to null.
                if (String.IsNullOrWhiteSpace(num)) num = null;
                if (String.IsNullOrWhiteSpace(email)) email = null;
                if (String.IsNullOrWhiteSpace(org)) org = null;

                //Set the values of the insert parameteres
                donationTracker_dataSource.InsertParameters["firstName"] =
                    new Parameter("firstName", System.Data.DbType.String, first);
                donationTracker_dataSource.InsertParameters["lastName"] =
                    new Parameter("lastName", System.Data.DbType.String, last);
                donationTracker_dataSource.InsertParameters["contactNumber"] =
                    new Parameter("contactNumber", System.Data.DbType.Int64, num);
                donationTracker_dataSource.InsertParameters["email"] =
                    new Parameter("email", System.Data.DbType.String, email);
                donationTracker_dataSource.InsertParameters["organization"] =
                    new Parameter("organization", System.Data.DbType.String, org);
                donationTracker_dataSource.InsertParameters["type"] =
                    new Parameter("type", System.Data.DbType.String, type);

                //Insert new record.
                donationTracker_dataSource.Insert();
                GridView1.ShowFooter = false;
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtEditFirstName =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditFirstName");
            TextBox txtEditLastName =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditLastName");
            TextBox txtEditContactNumber =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditContactNumber");
            TextBox txtEditEmail =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditEmail");
            TextBox txtEditOrganization =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditOrganization");
            DropDownList ddlEditDonorType =
                (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlEditDonorType");

            string f, l, n, m, o, t;
            f = txtEditFirstName.Text.Trim();
            l = txtEditLastName.Text.Trim();
            n = txtEditContactNumber.Text.Trim();
            m = txtEditEmail.Text.Trim();
            o = txtEditOrganization.Text.Trim();
            t = ddlEditDonorType.SelectedValue;

            if (String.IsNullOrWhiteSpace(n)) n = null;
            if (String.IsNullOrWhiteSpace(m)) m = null;
            if (String.IsNullOrWhiteSpace(o)) o = null;

            donationTracker_dataSource.UpdateParameters["firstName"] =
                    new Parameter("firstName", System.Data.DbType.String, f);
            donationTracker_dataSource.UpdateParameters["lastName"] =
                new Parameter("lastName", System.Data.DbType.String, l);
            donationTracker_dataSource.UpdateParameters["contactNumber"] =
                new Parameter("contactNumber", System.Data.DbType.Int64, n);
            donationTracker_dataSource.UpdateParameters["email"] =
                new Parameter("email", System.Data.DbType.String, m);
            donationTracker_dataSource.UpdateParameters["organization"] =
                new Parameter("organization", System.Data.DbType.String, o);
            donationTracker_dataSource.UpdateParameters["type"] =
                new Parameter("type", System.Data.DbType.String, t);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = false;
        }
    }
}